package com.powernode.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Category;
import com.powernode.domain.Prod;
import com.powernode.dto.ChangeDto;
import com.powernode.service.ProdService;
import com.powernode.utils.PowerAuthMenuUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/prod/prod")
@RestController
@Api(tags = "产品管理接口")
public class ProdController {


    @Autowired
    private ProdService prodService;

    @GetMapping("/page")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    @ApiOperation("产品管理")
    public ResponseEntity<Page<Prod>> getProdPage(Page<Prod> page, Prod prod) {
        page = prodService.getProdPage(page, prod);
        return ResponseEntity.ok(page);
    }


    @PostMapping
    @PreAuthorize("hasAuthority('prod:prod:save')")
    @ApiOperation("产品新增")
    public ResponseEntity<Void> addProd(@RequestBody Prod prod) {
        prodService.addProd(prod);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/info/{prodId}")
    public Prod getProdInfo(@PathVariable("prodId") Long prodId) {
        Prod prod = prodService.getById(prodId);
        return prod;
    }

    /**
     * 根据prodid获取商品
     *
     * @param ProdIds
     * @return
     */
    @PostMapping("/loadProdById")
    @ApiOperation("根据prodId获取商品")
    List<Prod> loadProdById(@RequestBody List<Long> ProdIds) {
        return prodService.listByIds(ProdIds);
    }


    /**
     * 小程序获取商品信息
     *
     * @param prodId
     * @return
     */
    @GetMapping("/prod/prodInfo")
    @ApiOperation(value = "商品信息")
    public ResponseEntity<Prod> getProdList(Long prodId) {
        Prod prod = prodService.getProdList(prodId);
        return ResponseEntity.ok(prod);
    }

    /**
     * 修改库存
     *
     * @param changeDto
     */
    @PostMapping("/changeDBOrder")
    @ApiOperation("根据id修改库存")
    public void changeDBOrder(@RequestBody ChangeDto changeDto) {
        prodService.changeDBOrder(changeDto);
    }

}
